package com.lzh.controller;


import com.github.pagehelper.PageInfo;
import com.lzh.pojo.College;
import com.lzh.pojo.Exam;
import com.lzh.pojo.Score;
import com.lzh.pojo.Student;
import com.lzh.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Reborn
 * @since 2020-06-19
 */
@Controller
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    StudentService studentService;

    @Autowired
    ScoreService scoreService;
    @Autowired
    CEService ceService;
    @Autowired
    ExamService examService;
    @Autowired
    CollegeService collegeService;


    @RequestMapping("/score")
    public String selScoreBySid(int sid, Integer pageNum, Integer pageSize, Model model) {
        PageInfo<Score> scorePageInfo = scoreService.queryScoreBysidPage(sid, pageNum, pageSize);
        Student student = studentService.selectStuByid(sid);
        College college = collegeService.QueryCollegeOne(student.getCId());
        model.addAttribute("college", college);
        model.addAttribute("type", "score");
        model.addAttribute("student", student);
        model.addAttribute("pageInfoL", scorePageInfo);
        return "student/score";
    }


    @RequestMapping("/toupd")
    public String toupd(Score score, Model model) {
        Score score1 = scoreService.queryByScore(score);
        System.out.println(score1);
        model.addAttribute("score", score1);
        model.addAttribute("type", "修改");
        return "student/UIExam";
    }

    @RequestMapping("/toaddd")
    public String toaddd(int sid, Model model) {
        Student student = studentService.selectStuByid(sid);
        List<Integer> integers = ceService.queryEByC(student.getCId());
        List<Score> scores = scoreService.queryScoreBysid(student.getId());
        LinkedList<Integer> list1 = new LinkedList<>();
        for (Integer integer : integers) {
            int a=1;
            for (Score score2 : scores) {
                if (integer==score2.getEid()){
                    a=2;
                    break;
                }
            }
            if (a==1){
                list1.add(integer);
            }
        }
        System.out.println(list1);
        List<Exam> exams = examService.queryEByids(list1);
        model.addAttribute("exams", exams);
        model.addAttribute("student", student);
        model.addAttribute("type", "添加");
        return "student/UIExam";
    }

    @RequestMapping("/updScore")
    public String updScore(Score score, Model model) {
        scoreService.updateScore(score);
        Score score1 = scoreService.queryByScore(score);
        model.addAttribute("score", score1);
        model.addAttribute("bj", "更新");
        return "student/success";
    }

    @RequestMapping("/adddScore")
    public String adddScore(Score score, Model model) {
        scoreService.addScore(score);
        Score score1 = scoreService.queryByScore(score);
        model.addAttribute("score", score1);
        model.addAttribute("bj", "添加");
        return "student/success";
    }
}

